<?php
// +----------------------------------------------------------------------
// | 供应链应用
// +----------------------------------------------------------------------
namespace app\schain\service;

use app\schain\model\SchainEnterpriseModel;

class EnterpriseService {

	public function adminEnterpriseList($filter) {
		return $this->adminPostList($filter);
	}

	public function adminPostList($filter, $isPage = false) {

		$where = [
			'a.create_time' => ['>=', 0],
			'a.delete_time' => 0,
		];

		$join = [
			['__USER__ u', 'a.user_id = u.id'],
		];

		$field = 'a.*,u.user_login,u.user_nickname,u.user_email';

		$category = empty($filter['category']) ? 0 : intval($filter['category']);
		if (!empty($category)) {
			$where['b.category_id'] = ['eq', $category];
			array_push($join, [
				'__SCHAIN_CATEGORY_ENTERPRISE__ b', 'a.id = b.ent_id',
			]);
			$field = 'a.*,b.id AS ent_category_id,b.list_order,b.category_id,u.user_login,u.user_nickname,u.user_email';
		}

		$startTime = empty($filter['start_time']) ? 0 : strtotime($filter['start_time']);
		$endTime = empty($filter['end_time']) ? 0 : strtotime($filter['end_time']);
		if (!empty($startTime) && !empty($endTime)) {
			$where['a.published_time'] = [['>= time', $startTime], ['<= time', $endTime]];
		} else {
			if (!empty($startTime)) {
				$where['a.published_time'] = ['>= time', $startTime];
			}
			if (!empty($endTime)) {
				$where['a.published_time'] = ['<= time', $endTime];
			}
		}

		//查询是否审核发布的企业：1为已发布，0为未发布
		$isPublished = empty($filter['is_published']) ? '' : $filter['is_published'];
		if (!empty($isPublished)) {
			$where['a.published_time'] = ['>', 0];
		}

		$keyword = empty($filter['keyword']) ? '' : $filter['keyword'];
		if (!empty($keyword)) {
			$where['a.ent_title'] = ['like', "%$keyword%"];
		}

		$schainPostModel = new SchainEnterpriseModel();
		$articles = $schainPostModel->alias('a')->field($field)
			->join($join)
			->where($where)
			->order('update_time', 'DESC')
			->paginate(12);

		return $articles;

	}

	public function publishedEnterprise($postId, $categoryId = 0) {
		$schainPostModel = new SchainEnterpriseModel();

		if (empty($categoryId)) {

			$where = [
				'post.ent_type' => 1,
				'post.published_time' => [['< time', time()], ['> time', 0]],
				'post.ent_status' => 1,
				'post.delete_time' => 0,
				'post.id' => $postId,
			];

			$article = $schainPostModel->alias('post')->field('post.*')
				->where($where)
				->find();
		} else {
			$where = [
				'post.ent_type' => 1,
				'post.published_time' => [['< time', time()], ['> time', 0]],
				'post.ent_status' => 1,
				'post.delete_time' => 0,
				'relation.category_id' => $categoryId,
				'relation.ent_id' => $postId,
			];

			$join = [
				['__SCHAIN_CATEGORY_ENTERPRISE__ relation', 'post.id = relation.ent_id'],
			];
			$article = $schainPostModel->alias('post')->field('post.*')
				->join($join)
				->where($where)
				->find();
		}

		return $article;
	}

	//上一篇企业
	public function publishedPrevEnterprise($postId, $categoryId = 0) {
		$schainPostModel = new SchainEnterpriseModel();

		if (empty($categoryId)) {

			$where = [
				'post.ent_type' => 1,
				'post.published_time' => [['< time', time()], ['> time', 0]],
				'post.ent_status' => 1,
				'post.delete_time' => 0,
				'post.id ' => ['<', $postId],
			];

			$article = $schainPostModel->alias('post')->field('post.*')
				->where($where)
				->order('id', 'DESC')
				->find();

		} else {
			$where = [
				'post.ent_type' => 1,
				'post.published_time' => [['< time', time()], ['> time', 0]],
				'post.ent_status' => 1,
				'post.delete_time' => 0,
				'relation.category_id' => $categoryId,
				'relation.ent_id' => ['<', $postId],
			];

			$join = [
				['__SCHAIN_CATEGORY_ENTERPRISE__ relation', 'post.id = relation.ent_id'],
			];
			$article = $schainPostModel->alias('post')->field('post.*')
				->join($join)
				->where($where)
				->order('id', 'DESC')
				->find();
		}

		return $article;
	}

	//下一篇企业
	public function publishedNextEnterprise($postId, $categoryId = 0) {
		$schainPostModel = new SchainEnterpriseModel();

		if (empty($categoryId)) {

			$where = [
				'post.ent_type' => 1,
				'post.published_time' => [['< time', time()], ['> time', 0]],
				'post.ent_status' => 1,
				'post.delete_time' => 0,
				'post.id' => ['>', $postId],
			];

			$article = $schainPostModel->alias('post')->field('post.*')
				->where($where)
				->order('id', 'ASC')
				->find();
		} else {
			$where = [
				'post.ent_type' => 1,
				'post.published_time' => [['< time', time()], ['> time', 0]],
				'post.ent_status' => 1,
				'post.delete_time' => 0,
				'relation.category_id' => $categoryId,
				'relation.ent_id' => ['>', $postId],
			];

			$join = [
				['__SCHAIN_CATEGORY_ENTERPRISE__ relation', 'post.id = relation.ent_id'],
			];
			$article = $schainPostModel->alias('post')->field('post.*')
				->join($join)
				->where($where)
				->order('id', 'ASC')
				->find();
		}

		return $article;
	}

}